package com.wy.tingshu.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wy.tingshu.util.DBUtil;
import com.wy.tingshu.view_object.UserVO;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 属于当前用户的未绑定的音频/查询所有候选音频列表
 * candidate:候选人
 **/
@WebServlet("/studio/album/candidate.json")
public class CandidateJsonServlet extends HttpServlet {

    private static class TrackView{
        public Integer tid;
        public String title;
        @SneakyThrows
        public TrackView(ResultSet rs){
            this.tid=rs.getInt("tid");
            this.title=rs.getString("title");

        }
    }
    private static class ResultView{
        public UserVO currentUser;
        public String aid;//为了add.js那好处理
        public List<TrackView> trackList=new ArrayList<>();
    }
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("aid");
        //获取当前登录用户
        HttpSession session=req.getSession(false);
        if(session==null){
            throw new  RuntimeException("未登录");
        }
        UserVO currentUser=(UserVO) session.getAttribute("currentUser");
        if(currentUser==null){
            throw new  RuntimeException("未登录");
        }
        ResultView resultView=new ResultView();
        resultView.currentUser=currentUser;
        resultView.aid=aid;//为了add.js那好处理
        try (Connection c= DBUtil.connection()){
            String sql="select tid,title from tracks where uid=? and tid not in (select tid from relations where aid = ? ) order by tid desc";
            try (PreparedStatement ps=c.prepareStatement(sql)){
                ps.setInt(1,currentUser.uid);
                ps.setString(2,aid);
                try (ResultSet rs= ps.executeQuery()){
                    while (rs.next()){
                        resultView.trackList.add(new TrackView(rs));
                    }

                }
            }
        }
        //转JSON
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(resultView);
        //响应
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);
        //http://127.0.0.1:8080/studio/album/candidate.json?aid=1此时访问aid为1的用户他的所有没绑定的候选音频就出来了
    }
}
